//
//  DDTabbarView.swift
//  swift微博
//
//  Created by whitcast on 16/7/9.
//  Copyright © 2016年 whitcast. All rights reserved.
//

import UIKit

class DDTabbarView: UITabBar {
    //闭包回调  撰写按钮
    var writeBtnClourse: (()->())?
    
    //懒加载撰写按钮
    lazy var btn :UIButton = {
        
        let btn = UIButton()
        //设置正常 和 选择状态下的图片
        btn.setImage(UIImage(named: "tabbar_compose_icon_add"), forState: UIControlState.Normal)
        
        btn.setImage(UIImage(named: "tabbar_compose_icon_add_highlighted"), forState: UIControlState.Selected)
        
        btn.setBackgroundImage(UIImage(named: "tabbar_compose_button"), forState: UIControlState.Normal)
        
        btn.setBackgroundImage(UIImage(named: "tabbar_compose_button_highlighted"), forState: UIControlState.Highlighted)
        //点击事件
        btn.addTarget(self, action: "click", forControlEvents: .TouchUpInside)
        
        return btn
        
    }()
    //撰写按钮的点击事件
   @objc private func click() {
        
        writeBtnClourse?()
    }
    
    //重写构造方法
    override init(frame: CGRect) {
        super.init(frame: frame)
        //添加按钮  撰写按钮
        addSubview(btn)
        
    }
    
    //使用xib时会自动调用
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    //设置子控件的frame
    override func layoutSubviews() {
        super.layoutSubviews()
        //修改btn的大小
        //使用下面的center,btn会抛出频幕之外
//                btn.center.x = center.x
//        
//                btn.center.y = center.y
        
        btn.center.x = frame.size.width * 0.5
        
        btn.center.y = frame.size.height * 0.5
        //按钮自适应大小
        btn.sizeToFit()
        
        //指示器
        var index = 0
        //宽
        let wid = frame.size.width / 5
        
        for u in subviews {
            //筛选出按钮
            if u.isKindOfClass(NSClassFromString("UITabBarButton")!) {
                
                u.frame.size.width = wid
                
                u.frame.origin.x = wid * CGFloat(index)
                
                if index == 1 {
                    
                    index++
                }
                
                index++
            }
        }
        
    }
    
    
    
    
    
    
    
    
    
    
    
}
